|
|
|
|
|
|
|
Using these equations, you can compute the real and imaginary parts of Zn in Visual Basic for different values of n. For instance, suppose you start with Z0 = 0 + 0 * i and C = 1 + 1 * i. Then the values for Zn would be: |
|
|
|
 |
|
|
|
|
Z0 = 0 + 0 * i
Z1 = (02 - 02 + 1) + (2 * 0 * 0 + 1) * i = 1 + 1 * i
Z2 = (12 - 12 + 1) + (2 * 1 * 1 + 1) * i = 1 + 3 * i
Z3 = (12 - 32 + 1) + (2 * 1 * 3 + 1) * i = -7 + 7 * i
Z4 = (72 - 72 + 1) + (2 * (-7) * 7 + 1) * i = 1 - 97 * i
Z5 = (12 - (-97)2 + 1) + (2 * 1 * (-97) + 1) * i = -9407 - 193 * i |
|
|
|
|
|
|
|
|
After this, the values for Rn and In become huge. In fact, it can be shown that, if the magnitude of Zn given by Sqr(Rn2 + In2) ever exceeds 2, the magnitude of the Zn values will eventually head toward infinity. |
|
|
|
|
|
|
|
|
The Mandelbrot set is a map showing how quickly the magnitude of Zn goes toward infinity for different values of C. Let the X and Y coordinates of a point correspond to the real and imaginary parts of the number C. Starting with Z0 = 0, compute the series of values Zn for that value of C. If you find a value for Zn that has magnitude greater than 2, you know the magnitudes will eventually head toward infinity. Use the number of iterations it took to find a magnitude greater than 2 to compute a color for that pixel. If you want to use NumColors colors and Zn is the first value that has a magnitude greater than 2, you would give the pixel color number n Mod NumColors. |
|
|
|
|
|
|
|
|
For some values of C, the magnitude of the Zn will not grow toward infinity. After you have examined some predefined number of values, stop. All the values of C that make you stop in this way end up with the same color. If the maximum number of Zn you compute is a multiple of the number of colors you use, these pixels will all be colored with color number 0. For example, if you are using 16 colors and you compute up to 64 values for Zn, all these pixels will have color number 64 Mod 16 = 0. |
|
|
|
|
|
|
|
|
The following code shows a Mandelbrot set drawing routine. This routine uses the GetBitmapPixels and SetBitmapPixels subroutines described in Chapter 3, Advanced Color, to update the image quickly. |
|
|
|
 |
|
|
|
|
' Draw the Mandelbrot set. Private Sub DrawMandelbrot() ' Work until the magnitude squared > 4. Const MAX_MAG_SQUARED = 4
Dim pixels() As RGBTriplet
Dim bits_per_pixel As Integer
Dim wid As Long
Dim hgt As Long
Dim clr As Long
Dim i As Integer
Dim j As Integer
Dim ReaC As Double
Dim ImaC As Double |
|
|
|
|
|